package link.anyauto.weclass.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;

import link.anyauto.weclass.BR;
import link.anyauto.weclass.R;

/**
 * Created by discotek on 17-2-14.
 */

public class OneTypeItemSourcePagerAdapter extends PagerAdapter {

    OneTypeItemSource source;
	LinkedList<View> views = new LinkedList<>();
	LayoutInflater inflater;

	ObservableList.OnListChangedCallback callback = new ObservableList.OnListChangedCallback() {
		@Override
		public void onChanged(ObservableList observableList) {
			notifyDataSetChanged();
		}

		@Override
		public void onItemRangeChanged(ObservableList observableList, int i, int i1) {
			notifyDataSetChanged();
		}

		@Override
		public void onItemRangeInserted(ObservableList observableList, int i, int i1) {
			notifyDataSetChanged();
		}

		@Override
		public void onItemRangeMoved(ObservableList observableList, int i, int i1, int i2) {
			notifyDataSetChanged();
		}

		@Override
		public void onItemRangeRemoved(ObservableList observableList, int i, int i1) {
			notifyDataSetChanged();
		}
	};
	
	public OneTypeItemSourcePagerAdapter(OneTypeItemSource source, LayoutInflater inflater) {
		this.inflater = inflater;
		this.source = source;
		if(source.items != null) {
			this.source.items.removeOnListChangedCallback(callback);
			this.source.items.addOnListChangedCallback(callback);
		}
	}

	void recycleView(View view) {
		views.add(view);
	}
	
	View getCachedView() {
		if(!views.isEmpty()) {
			return views.removeFirst();
		}
		return null;
	}
	
    @Override
    public int getCount() {
        return source == null || source.items == null ? 0 : source.items.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
		views.add((View) object);
	}
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View cache = getCachedView();
		if(cache == null) {
			ViewDataBinding binding = DataBindingUtil.inflate(inflater, source.itemViewId, container, false);
			cache = binding.getRoot();
			cache.setTag(R.id.binding, binding);
			PositionListeners pl = new PositionListeners();
			pl.listeners = source.listeners;
			cache.setTag(R.id.pl, pl);
		}

		ViewDataBinding binding = (ViewDataBinding) cache.getTag(R.id.binding);
		binding.setVariable(BR.item, source.items.get(position));
		PositionListeners pl = (PositionListeners) cache.getTag(R.id.pl);
		pl.position = position;
		pl.listeners = source.listeners;
		try {
			binding.setVariable(BR.pl, pl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		container.addView(cache);
		return cache;
	}
}
